/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package com.north.king.profile.http.client.resttemplate.server.data;

import com.north.king.profile.http.client.resttemplate.domain.IntegerRange;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * class info
 *
 * @author hujia
 * @date 2018/12/7 14:28
 */
@Service
@Slf4j
public class CreateConditionData {

    private List<List<IntegerRange>> userList = new ArrayList<>();


    public Map<String, String> conditionDesignated(int number) {
        if (userList == null || userList.size() < number) {
            log.error("error in create condition.the userList size too small.");
        }
        Map<String, String> result = new LinkedHashMap<>();
        result.put("roleName", "role" + number);
        List<IntegerRange> tmp = userList.get(number);
        try {
            for (int i = 0; i < tmp.size(); ++i) {
                Pair<String, String> tmpPair = createKeyDataOneByOne(tmp.get(i).getMax(), tmp.get(i).getBase(), i);
                result.put(tmpPair.getKey(), tmpPair.getValue());
            }
        } catch (Exception e) {
            log.info(e.toString());
        }
        return result;
    }


    public Pair<String, String> createKeyDataOneByOne(int maxNumber, int base, int level) {
        String[] levelName = {"orgBusiness", "workqueue", "busiType", "data"};
        int i = 0;
        StringBuilder keys = new StringBuilder();
        while (i < maxNumber) {
            Integer tmpNumber = i + base;
            keys.append(levelName[level]);
            keys.append(tmpNumber + ",");
            ++i;
        }
        return new Pair<>(levelName[level], keys.substring(0, keys.length() - 1));
    }

    public void createUser() {
        int[] user1 = {1, 20};
        addUser(user1.length / 2, user1);
        int[] user2 = {21, 30};
        addUser(user2.length / 2, user2);
        int[] user3 = {31, 40};
        addUser(user3.length / 2, user3);
        int[] user4 = {41, 50};
        addUser(user4.length / 2, user4);
        int[] user5 = {1, 20, 1, 30};
        addUser(user5.length / 2, user5);
        int[] user6 = {1, 20, 1, 30};
        addUser(user6.length / 2, user6);
        int[] user7 = {1, 20, 31, 60};
        addUser(user7.length / 2, user7);
        int[] user8 = {1, 20, 31, 60};
        addUser(user8.length / 2, user8);
        int[] user9 = {21, 40, 61, 80};
        addUser(user9.length / 2, user9);
        int[] user10 = {21, 40, 61, 80};
        addUser(user10.length / 2, user10);
        int[] user11 = {41, 50, 1, 80};
        addUser(user11.length / 2, user11);
        int[] user12 = {41, 50, 1, 80};
        addUser(user12.length / 2, user12);
        int[] user13 = {1, 10, 1, 30, 1, 30};
        addUser(user13.length / 2, user13);
        int[] user14 = {11, 20, 31, 60, 31, 50};
        addUser(user14.length / 2, user14);
        int[] user15 = {21, 30, 21, 40, 51, 80};
        addUser(user15.length / 2, user15);
        int[] user16 = {31, 40, 41, 60, 81, 100};
        addUser(user16.length / 2, user16);
        int[] user17 = {1, 20, 31, 50, 1, 30, 1, 30};
        addUser(user17.length / 2, user17);
        int[] user18 = {21, 40, 1, 20, 21, 50, 11, 40};
        addUser(user18.length / 2, user18);
        int[] user19 = {41, 50, 31, 60, 51, 70, 51, 60};
        addUser(user19.length / 2, user19);
        int[] user20 = {31, 40, 1, 20, 21, 50, 11, 40};
        addUser(user10.length / 2, user10);
    }

    public void clearUser() {
        userList.clear();
    }

    private void addUser(int count, int[] array) {
        List<IntegerRange> user = new ArrayList<>();
        for (int i = 0; i < count; ++i) {
            user.add(new IntegerRange(array[2 * i] - 1, array[2 * i + 1] - array[2 * i] + 1));
        }
        userList.add(user);
    }
}
